package org.hot100_6.leetcode128;

import java.util.HashSet;

public class Solution {
    public int longestConsecutive(int[] nums) {

        if (nums.length == 0 || nums.length == 1) {
            return nums.length;
        }

        HashSet<Integer> hashSet = new HashSet<>();
        int maxLength = 1;

        for (int num : nums) {
            hashSet.add(num);
        }

        //遍历的是hashSet，不是nums，如果nums中的重复的数据较多，会导致超时
        for (int num : hashSet) {
            int currentLength = 1;

            //判断当前是某个序列的最小值
            if (!hashSet.contains(num - 1)) {
                while (hashSet.contains(num + 1)) {
                    num++;
                    currentLength++;
                }
            }
            maxLength = Math.max(maxLength, currentLength);
        }


        return maxLength;
    }
}
